const express = require("express");
const cors = require("cors");
const getRankData = require("./getIcon");
const https = require("https");

const app = express();
app.use(cors());
app.get("/api/square", (req, res) => {
  // req.query
  //   let rankedData = JSON.stringify(getRankData());
  //   console.log(rankedData);
  let rankedData = getRankData();
  //返回内容
  res.send(rankedData);
});

app.get("/api/follower", (req1, res) => {
  const options = {
    hostname: "api.bilibili.com",
    port: 443,
    path: "/x/relation/stat?vmid=89088263",
    method: "GET",
  };

  const req = https.request(options, (response) => {
    console.log(`状态码: ${res.statusCode}`);

    response.on("data", (d) => {
      let followerNum = JSON.parse(d.toString());
      console.log("粉丝数：", followerNum);
      res.send(followerNum);
    });
  });

  req.on("error", (error) => {
    console.error(error);
    res.send("");
  });

  req.end();
});

app.listen(8765, () => {
  console.log("http://127.0.0.1:8765");
});
